package com.ccit.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccit.common.client.CourseClient;
import com.ccit.common.exception.GymException;
import com.ccit.course.mapper.CourseOrderMapper;
import com.ccit.course.repository.CourseOrderRepository;
import com.ccit.course.service.CourseOrderService;
import com.ccit.model.Course;
import com.ccit.model.CourseOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Transactional(rollbackFor = Exception.class)
public class CourseOrderServiceImpl extends ServiceImpl<CourseOrderMapper, CourseOrder> implements CourseOrderService {

    @Autowired
    private CourseOrderMapper courseOrderMapper;

    @Autowired
    private CourseClient courseClient;

    @Autowired
    private CourseOrderRepository courseOrderRepository;

    @Override
    public Boolean saveOrder(CourseOrder order) {
       /* Course courseExist = courseClient.getCourseById(order.getCourseId());
        if (courseExist != null){*/
        Optional<CourseOrder> one = courseOrderRepository.findById(order.getId());
            if (one != null&&one.isPresent()){
                CourseOrder orderExistMongo = one.get();
                order.setTime(orderExistMongo.getTime());
                CourseOrder save = courseOrderRepository.save(order);
//                if (save!=null){
//                    return true;
//                }else return false;
            }else {//订单不存在
                order.setTime(new Date());
                CourseOrder save = courseOrderRepository.save(order);
//                if (save!=null){
//                    return true;
//                }else return false;
            }
        CourseOrder orderExist = courseOrderMapper.selectById(order.getId());

        if (orderExist != null) {
            order.setTime(orderExist.getTime());
            courseOrderMapper.updateById(order);
        } else {
            order.setTime(new Date());
            courseOrderMapper.insert(order);
        }
        return true;
        /*}else {
            throw new GymException(202,"参数错误，无此课程");
        }*/

    }

    @Override
    public List<CourseOrder> findAllOrder() {
        Sort sort = Sort.by(Sort.Direction.DESC, "time");
        List<CourseOrder> all = courseOrderRepository.findAll(sort);
        return all;
    }

    @Override
    public List<CourseOrder> findOrderList(CourseOrder order) {
        Sort sort = Sort.by(Sort.Direction.DESC, "time");
        //构建条件
        Example<CourseOrder> orderExample = Example.of(order);
        List<CourseOrder> orderList = courseOrderRepository.findAll(orderExample, sort);
        return orderList;
    }

    @Override
    public List<CourseOrder> findOrderLike(CourseOrder order) {
        Sort sort = Sort.by(Sort.Direction.DESC, "time");
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()//构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true);//改变默认大小写忽略方式：忽略大小写
        Example<CourseOrder> orderExample = Example.of(order);
        List<CourseOrder> orderList = courseOrderRepository.findAll(orderExample, sort);
        return orderList;
    }

    @Override
    public List<CourseOrder> findOrderByPhone(String phone) {
        return courseOrderRepository.findCourseOrderByMemberPhoneOrderByTime(phone);
    }

    @Override
    public List<CourseOrder> listAllCourseOrder() {
        return courseOrderMapper.listAllCourseOrder();
    }

    @Override
    public Page<CourseOrder> getByPage(Integer current, Integer size) {
        QueryWrapper<CourseOrder> wrapper = new QueryWrapper<CourseOrder>();
        wrapper.lambda()
                .orderByDesc(CourseOrder::getTime);
        Page<CourseOrder> page = new Page<>(current, size);
        Page<CourseOrder> courseOrderPage = courseOrderMapper.selectPage(page, wrapper);
        return courseOrderPage;
    }

    @Override
    public List<CourseOrder> getByPhone(IPage<CourseOrder> courseOrderIPage, String phone) {
        List<CourseOrder> courseOrder = courseOrderMapper.getByPhone(courseOrderIPage, phone);
        return courseOrder;
    }


}
